new Vue({
    el:'#app',
    data: {
        shoppingcarts:[],
        selectedgoods:[],
        uid:1,
        person:1,
        hastopay:false,
        typenum : 0,
        goodsnum : 0,
        allprice : 0,
    },
    created(){
        this.uid=localStorage.getItem("user_id")
        console.log(this.uid)
        axios({
            method: 'get',
            url: 'http://47.94.138.204:7790/example/findshoppingcar',
            params:{
                uid : this.uid,
            },
        }).then((res) => {
            let data = res.data;
            this.shoppingcarts = data;
        })

    },
    methods:{
        deleteCart:function (gid, index) {
            axios({
                method: 'get',
                url: 'http://47.94.138.204:7790/example/deleteshoppingcar',
                params:{
                    uid : this.uid,
                    gid: gid,
                },
            }).then((res) => {
                let data = res.data;
                this.shoppingcarts.splice(index,1);
            })

        },
        gotohadpay:function(){
            if(this.selectedgoods.length > 0)
            {
                window.location.href = "payment.html?uid=" + this.uid;
            }else{
                alert("您的购物车还没有添加商品。");
                this.hastopay = false;
            }

        },
        pay:function () {




            this.hastopay = true;

            let goods = document.getElementsByName("payornot");
            for(let i = 0; i < goods.length; i++){
                if (goods[i].checked == true){
                    this.selectedgoods.push(this.shoppingcarts[i]);
                    this.typenum = this.typenum + 1;
                    this.goodsnum = this.goodsnum + this.shoppingcarts[i].num;
                    this.allprice = this.allprice + this.shoppingcarts[i].allPrice;

                }
            }
            console.log(this.selectedgoods);
            for(let i=0;i<this.selectedgoods.length;i++)
            {
                axios({
                    method:'get', //post
                    url: 'http://47.94.138.204:7790/order/addorder',
                    params:{
                        // 得到的数据以json类型存放
                        uid:this.uid,
                        gid:this.selectedgoods[i].sid,
                        num:this.selectedgoods[i].num

                    },
                }).then((res)=>{
                    let result = res.data
                    console.log("typelist")
                    console.log(result)

                    this.typelist = result;

                })
            }







        }
    }
})